package com.xdf.imoocdatav.utils;


import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @ClassName:GlobalExceptionHandler
 * @Description:TODO
 * @Author:chanchaw
 * @Date:2019-12-23 8:37
 * @Version:1.0
 **/
@ControllerAdvice
public class GlobalExceptionHandler {
    // 在请求接口反序列化之前将字符串类型的日期时间参数根据指定格式转换为日期类型
//    @InitBinder
//    public void initBinder(WebDataBinder binder) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
//    }

    //@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    // 返回这个错误码会导致前端认为服务器内部错误 - INTERNAL_SERVER_ERROR是500表示服务器内部错误
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    public JsonResult handleBusinessException(BusinessException ex) {
        String msg = ex.getMsg();
        return JsonResult.error(msg);
    }

}
